<?php
	include "../public/commom/config.php";
	include "../public/commom/commom.php";
	
	if(isset($_SESSION["area_id"])) {
		if(isset($_GET["area_id"])) {
			$_SESSION["area_id"] = $_GET["area_id"];
			$_SESSION["area_name"] = $_GET["area_name"];
		}
	}else {
		$sql = "SELECT * FROM area LIMIT 1";
		
		$result = mysqli_query($conn, $sql);
		
		while($row = mysqli_fetch_assoc($result)) {
			$area_id = $row["id"];
			$aname = $row["name"];
		}
		
		$_SESSION["area_id"] = $area_id;
		$_SESSION["area_name"] = $aname;
		
	}
	
	$aname = $_SESSION["area_name"];
?>

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="Keywords" content="" />
		<meta name="Description" content="" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
		<title>飞花</title>
		<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
		<link rel="stylesheet" href="public/fontawesome-free-5.8.2-web/css/all.css">
		<link rel="stylesheet" type="text/css" href="public/css/index.css"/>
		<script src="public/js/index.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<div id="header">
			<div class="area">
				<span><a href="area.php">选择地区</a>&nbsp;&nbsp;&nbsp;<?php echo $aname; ?></span>
			</div>
			<div class="search">
				<span><a href="javascript:;">搜索</a></span>
			</div>
		</div>
		<div id="main">
			<div class="advert">
				<ul>
					<?php
						$sql = "SELECT * FROM advert";
						
						$result = mysqli_query($conn, $sql);
						
						while($row = mysqli_fetch_assoc($result)) {
					        echo "<li>
						       		<a href='" . $row["url"] . "'><img src='" . $imgPath . $row["img"] . "'/></a>
						        </li>";
					    }
					?>
				</ul>
			</div>
			<div class="class">
				<ul>
					<?php
						$sql = "SELECT * FROM class LIMIT 3";
						
						$result = mysqli_query($conn, $sql);
						
						while($row = mysqli_fetch_assoc($result)) {
					        echo "<li>
					        		<a href='class/index.php?class_id=" . $row["id"] . "'><img class='rounded-circle' src='" . $imgPath . $row["img"] . "'/></a>
									<h6>" . $row["name"] . "</h6>
								</li>";
					    }
					?>
				</ul>
			</div>
			<div class="public">
				<div class="public-title">
					<h5>店铺</h5>
					<span><a href="morestore.php">更多&gt;</a></span>
				</div>
				<div class="public-img">
					<ul>
						<?php
							$sql = "SELECT * FROM store LIMIT 10";
						
							$result = mysqli_query($conn, $sql);
							
							while($row = mysqli_fetch_assoc($result)) {
						        echo "<li>
							        	<a href='store.php?store_id=" . $row["id"] . "'><img src='" . $imgPath . $row["logo"] . "'/></a>
							        	<h6>" . $row["name"] . "</h6>
							        </li>";
						    }	
						?>
					</ul>
				</div>
			</div>
			<?php
				$csql = "SELECT * FROM class";
						
				$cresult = mysqli_query($conn, $csql);
				
				while($row = mysqli_fetch_assoc($cresult)) {
					$ssql = "SELECT shop.* FROM shop LEFT JOIN brand ON (shop.brand_id = brand.id) LEFT JOIN class ON (brand.class_id = class.id) LEFT JOIN store ON (shop.store_id = store.id) LEFT JOIN user ON (store.user_id = user.id) WHERE brand.class_id = " . $row['id'] . " AND shop.area_id = " . $_SESSION["area_id"] . " LIMIT 10";
					
					$sresult = mysqli_query($conn, $ssql);
					
					if(mysqli_num_rows($sresult)) {
						echo "<div class='public'>
								<div class='public-title'>
									<h5>" . $row["name"] . "推荐</h5>
									<span><a href='recommend.php?class_id=" . $row["id"] . "'>更多&gt;</a></span>
								</div>";
								
						echo "	<div class='public-img'>
									<ul>";
						while($row = mysqli_fetch_assoc($sresult)) {
							echo "<li>
									<a href='shopdetails.php?shop_id=" . $row["id"] . "'><img src='" . $imgPath . $row["img"] . "'></a>
									<h6>" . $row["name"] . "</h6>
								</li>";
						}
						echo "		</ul>
								</div>
							</div>";
					}
			        
			    }
			?>
			<div class="news">
				<div class="public-title">
					<h5>新闻</h5>
					<span><a href="morenews.php">更多&gt;</a></span>
				</div>
				<div class="news-content">
					<ul>
						<?php
							$sql = "SELECT news.*, class.name cname FROM news LEFT JOIN class ON (news.class_id = class.id) LIMIT 5";
						
							$result = mysqli_query($conn, $sql);
							
							while($row = mysqli_fetch_assoc($result)) {
						        echo "<li>
							        	<b>[<a href='more.php?class_id=" . $row["class_id"] . "'>" . $row["cname"] . "</a>]</b>&nbsp;
							        	<a href='newsdetails.php?id=" . $row["id"] . "'>" . $row["title"] . "</a>
							        	<span class='right'>" . date("Y-m-d", $row["time"]) . "</span>
						        	</li>";
						    }
						    
						    mysqli_close($conn);
						?>
					</ul>
				</div>
			</div>
		</div>
		<!--<div id="footer"></div>-->
	</body>

</html>